package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IEmployeeService employeeService;



    private IDepartmentService departmentService;

    @Autowired
    public void setDepartmentService(IDepartmentService departmentService) {
        this.departmentService = departmentService;
    }

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo) {
        PageInfo<Employee> pageInfo = employeeService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

//        回显公告信息


        List<Department> list = departmentService.getList();
        model.addAttribute("departments",list);

//        employee/list.jsp
        return "/employee/list";
    }
    @RequestMapping("/input")
    public String input(Long id ,Model model){

        //部门回显
        List<Department> list = departmentService.getList();
        model.addAttribute("departments",list);
        //角色回显
                List<Role> roles =  roleService.getList();
                model.addAttribute("roles",roles);
        if(id!=null){
            //员工数据回显
        Employee employee = employeeService.get(id);
        model.addAttribute("employee",employee);
//      拥有的角色回显
          List<Role> emp_role = roleService.getRole(id);
          model.addAttribute("emp_role",emp_role);
        }
        return "employee/input";
    }

    @RequiredPermission(name="员工删除",expression = "Employee.list")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if(id!=null){
        employeeService.delete(id);
        }
        return "redirect:/employee/list";
    }



    @RequiredPermission(name="员工新增修改",expression = "Employee.updateOrInsert")
    @RequestMapping("/updateOrInsert")
    public String updateOtInsert(Employee employee,Long[] ids){

     if(employee.getId()!=null){
         employeeService.update(employee,ids);
     }
     else{
         employeeService.save(employee,ids);
     }

        return "redirect:/employee/list";
    }
@RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws IOException {
    String fileName = new String("员工通讯录.xls".getBytes("UTF-8"), "ISO-8859-1");
        response.setHeader("Content-Disposition","attachment;filename="+fileName);
        employeeService.exportXls(response.getOutputStream());

    }
    @RequestMapping("/importXls")
    public String importXls(MultipartFile file) throws IOException {
        employeeService.importXls(file);
      return "redirect:/employee/list";
        }

}

